package com.ifast.asset.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

import com.ifast.asset.domain.Changerecord;
import com.ifast.asset.domain.ChangerecordDO;
import com.ifast.asset.service.ChangerecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-03-11 14:25:14 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/changerecord")
public class ChangerecordController extends BaseController {
	@Autowired
	private ChangerecordService changerecordService;
	
	@GetMapping()
	@RequiresPermissions("wxmp:changerecord:changerecord")
	String Changerecord(){
	    return "wxmp/changerecord/changerecord";
	}
	
	@ResponseBody
	@PostMapping("/list")
	//@RequiresPermissions("wxmp:changerecord:changerecord")
	Result<Page<ChangerecordDO>> list(@RequestBody  Changerecord changerecord) {
        Wrapper<ChangerecordDO> wrapper = new EntityWrapper().orderBy("id", false);
		SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (!"".equals(changerecord.getMsg())&&changerecord.getMsg()!=null){
			wrapper.like("msg",changerecord.getMsg());
		}
		if (!"".equals(changerecord.getIp())&&changerecord.getIp()!=null){
			wrapper.eq("ip",changerecord.getIp());
		}
		try{
			if (!"".equals(changerecord.getStartTime())&&!"".equals(changerecord.getEndTime())&&changerecord.getStartTime()!=null&&changerecord.getEndTime()!=null){
				wrapper.between("time",(simdate.parse(changerecord.getStartTime())),(simdate.parse(changerecord.getEndTime())));
			}
		}catch (Exception e){

			e.printStackTrace();
		}

        Page<ChangerecordDO> page = changerecordService.selectPage(getPage(ChangerecordDO.class,changerecord.getPageNum(),changerecord.getPageSize()), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	//@RequiresPermissions("wxmp:changerecord:add")
	String add(){
	    return "wxmp/changerecord/add";
	}

	@GetMapping("/edit/{id}")
	//@RequiresPermissions("wxmp:changerecord:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		ChangerecordDO changerecord = changerecordService.selectById(id);
		model.addAttribute("changerecord", changerecord);
	    return "wxmp/changerecord/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wxmp:changerecord:add")
	public Result<String> save( ChangerecordDO changerecord){
		changerecordService.insert(changerecord);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wxmp:changerecord:edit")
	public Result<String>  update( ChangerecordDO changerecord){
		boolean update = changerecordService.updateById(changerecord);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("wxmp:changerecord:remove")
	public Result<String>  remove( Integer id){
		changerecordService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wxmp:changerecord:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		changerecordService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
